# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=osgworks
pkgbase=mingw-w64-${_realname}-git
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-git" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug-git")
_ver_base=3.1.0
pkgver=3.1.0.444
pkgrel=3
pkgdesc="A toolkit for OpenSceneGraph applications (mingw-w64)"
arch=('any')
license=('custom')
url="https://github.com/mccdo/osgworks/"
depends=("${MINGW_PACKAGE_PREFIX}-OpenSceneGraph" "${MINGW_PACKAGE_PREFIX}-vrpn")
makedepends=("git" "${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-cmake")
options=(!strip staticlibs !buildflags)
source=("${_realname}"::"git+https://github.com/mccdo/osgworks.git"
        "osg-3.3.x.patch"
        "osg-3.6.2.patch"
        "mingw-fpc-libs.patch")
sha256sums=('SKIP'
            '8a05e43a4947cb4dddb77ab8d20e806cb00abaa0ecfa677b3df5989562da5e4e'
            '220c5960e447e1ebc7208bea6e58a6211beca2f9e3bebea14719ec8721911193'
            '1de62ae24ee5dbe53ab51d9b4f9b362427b05f7e6a42083287388aad6cd785a1')

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "%s.%s" "${_ver_base}" "$(git rev-list --count HEAD)"
}

prepare() {
  cd "${srcdir}/${_realname}"
  patch -p1 -i ${srcdir}/osg-3.3.x.patch
  patch -p1 -i ${srcdir}/osg-3.6.2.patch
  patch -p1 -i ${srcdir}/mingw-fpc-libs.patch
}

build() {
  [[ -d ${srcdir}/build-release ]] && rm -rf ${srcdir}/build-release
  mkdir -p ${srcdir}/build-release && cd ${srcdir}/build-release

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    ../${_realname}

  make

  [[ -d ${srcdir}/build-debug ]] && rm -rf ${srcdir}/build-debug
  mkdir -p ${srcdir}/build-debug && cd ${srcdir}/build-debug

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Debug \
    ../${_realname}

  make
}

package_release() {
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-${_realname}-svn")
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-svn)

  cd ${srcdir}/build-release

  make DESTDIR=${pkgdir} install
}

package_debug() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-git=${pkgver}" "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph-debug")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-debug" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug-svn")
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-debug-svn)

  cd ${srcdir}/build-debug

  make DESTDIR=${pkgdir} install

  rm -rf ${pkgdir}${MINGW_PREFIX}/include
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/flagpoll
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/*.cmake
  rm -rf ${pkgdir}${MINGW_PREFIX}/share
}

package_mingw-w64-i686-osgworks-git() {
  package_release
}

package_mingw-w64-i686-osgworks-debug-git() {
  package_debug
}

package_mingw-w64-x86_64-osgworks-git() {
  package_release
}

package_mingw-w64-x86_64-osgworks-debug-git() {
  package_debug
}

